﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using log4net;


namespace RecommenderSystem.misc
{
    public class User
    {

        private IDictionary<string, double> items;
        private double averageRate;
        private string userId;



        public User(string userId)
        {
            this.userId = userId;
            items = new Dictionary<string, double>();
            averageRate = 0.0;
        }

        public string getUserId()
        {
            return userId;
        }


        public Boolean addItem(string itemId, string itemRate)
        {
            if (!items.ContainsKey(itemId))
            {
                double parsedItemRate = 0.0;
                try
                {
                    parsedItemRate = double.Parse(itemRate);
                }
                catch (Exception ex)
                {
//                    logger.Warn("Failed to parse item: " + itemId + " rating. ", ex);
                }
                items.Add(itemId, parsedItemRate);
                return true; ;
            }
            return false;
        }

        public double getAverageRating()
        {
            if (averageRate == 0.0)
            {
                averageRate = items.Values.Average();
            }
            return averageRate;
        }

        public Dictionary<double, int> GetRatingsHistogram()
        {
            Dictionary<double, int> d = new Dictionary<double, int>();
            for (int i = 1; i <= 5; i++)
            {
                d[i] = 0;
            }
            foreach (double val in this.items.Values)
            {
                d[val]++;
            }
            return d;
        }


        public ICollection<string> getRatedItemsIds()
        {
            return items.Keys;
        }

        public double getItemRate(string itemId)
        {
            return items.ContainsKey(itemId) ? items[itemId] : -1;
        }
    }
}
